//
//  ZLNavigationRoot.swift
//  ZLNavigationRoot
//
//  Created by 赵振磊 on 2023/1/8.
//

import UIKit
import SnapKit
import ZLExtension

public class ZLNavigationRoot: UINavigationController {
    /// 监控导航隐藏状态
    public override var isNavigationBarHidden: Bool {
        didSet {
            self.layoutContentViews()
        }
    }
    /// 导航背景色
    var navBackgroundColor: UIColor = UIColor.white {
        didSet {
            self.updateNavStyle()
        }
    }
    /// 导航标题色
    var navTitleColor: UIColor = UIColor.black {
        didSet {
            self.updateNavStyle()
        }
    }
    /// 底部线颜色
    var bottomLineColor: UIColor = UIColor.clear {
        didSet {
            self.updateNavStyle()
        }
    }
    /// 底部线
    private lazy var bottomLine: UIView = {
        if let backView = self.navigationBar.subviews.first {
            for sonView in backView.subviews {
                if sonView.frame.height <= 1 {
                    return sonView
                }
            }
        }
        return UIView()
    }()
    /// 根控制器
    var rootVC: UIViewController?
    /// 导航显示与隐藏
    public override func setNavigationBarHidden(_ hidden: Bool, animated: Bool) {
        super.setNavigationBarHidden(hidden, animated: animated)
        self.layoutContentViews()
    }
    public override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        self.rootVC = rootViewController
        self.modalPresentationStyle = .custom
        self.interactivePopGestureRecognizer?.delegate = self
        self.updateNavStyle()
    }
    /// 更新样式
    func updateNavStyle() {
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = self.navBackgroundColor
            appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: self.navTitleColor]
            appearance.shadowColor = self.bottomLineColor
            navigationBar.standardAppearance = appearance
            navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
        } else {
            UINavigationBar.appearance().barTintColor = self.navBackgroundColor
            UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: self.navTitleColor]
            UINavigationBar.appearance().tintColor = self.navTitleColor
            self.bottomLine.backgroundColor = self.bottomLineColor
        }
    }
    /// 更新contentView布局
    func layoutContentViews() {
        self.viewControllers.forEach { vc in
            if let rootVC = vc as? ZLBaseViewController {
                rootVC.layoutContentView()
            }
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension ZLNavigationRoot: UIGestureRecognizerDelegate {
    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        guard let topVC = self.topViewController as? ZLBaseViewController else {
            return self.viewControllers.count > 1
        }
        return topVC.gestureBack
    }
}
